﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.DataMaintenance
{
    public class SYuanQuWeiHu : IYuanQuWeiHu
    {
        private readonly ISqlSugarClient db;
        public SYuanQuWeiHu(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 院区维护添加
        /// </summary>
        /// <param name="YuanQuAddClass"></param>
        /// <returns></returns>
        public string PostYuanQuAdd(YuanQuAddClass YuanQuAddClass)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; int iShunXu = 0;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{YuanQuAddClass}】");

                #region 数据校验

                if (string.IsNullOrEmpty(YuanQuAddClass.strYuanQuMingCheng) || string.IsNullOrEmpty(YuanQuAddClass.strYuanQuTiJianHaoQianZhui) || string.IsNullOrEmpty(YuanQuAddClass.iTiJianHaoQiShiShuZi.ToString())||string.IsNullOrEmpty(YuanQuAddClass.iTiaoMaHaoQiShiShuZi.ToString())||
                    string.IsNullOrEmpty(YuanQuAddClass.iTiJianHaoQiShiShuZi.ToString()) ||string.IsNullOrEmpty(YuanQuAddClass.strTiaoMaHaoQianZhui)||string.IsNullOrEmpty(YuanQuAddClass.strShiFouQiYong) ||string.IsNullOrEmpty(YuanQuAddClass.strShiFouZhuKu))
                {
                    nret = 1;
                    strFanHuiZhi = "关键值为空，请检查!";
                    goto PROC_EXIT;
                }

                #endregion

                #region 判断是否存在相同名称的院区

                List<jcsj_YuanQu> ListYuanQu = new SqlSugarRepository<jcsj_YuanQu>(db).getByWhere(a => a.YuanQuMingCheng == YuanQuAddClass.strYuanQuMingCheng);

                if (ListYuanQu.Count!=0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前院区【{YuanQuAddClass.strYuanQuMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 获得顺序

                List<jcsj_YuanQu> ListXinXi = new SqlSugarRepository<jcsj_YuanQu>(db).getAll();

                if (ListXinXi.Count != 0)
                {
                    iShunXu = ListXinXi.Max(t => t.ShunXu) + 1;
                }
                else
                {
                    iShunXu = 1;
                }


                #endregion

                #region 执行添加数据库

                jcsj_YuanQu YuanQu = new jcsj_YuanQu();

                YuanQu.YuanQuMingCheng = YuanQuAddClass.strYuanQuMingCheng;
                YuanQu.TiJianHaoQianZhui= YuanQuAddClass.strYuanQuTiJianHaoQianZhui;
                YuanQu.TiJianHaoQiShi = YuanQuAddClass.iTiJianHaoQiShiShuZi;
                YuanQu.TiaoMaHaoQianZhui = YuanQuAddClass.strTiaoMaHaoQianZhui;
                YuanQu.TiaoMaHaoWeiShu = YuanQuAddClass.iTiaoMaHaoWeiShu;
                YuanQu.TiaoMaHaoQiShi = YuanQuAddClass.iTiaoMaHaoQiShiShuZi;
                YuanQu.ShiFouQiYong = YuanQuAddClass.strShiFouQiYong;
                YuanQu.ShiFouZhuKu = YuanQuAddClass.strShiFouZhuKu;
                YuanQu.ShunXu = iShunXu;

                BolShiFouChengGong = new SqlSugarRepository<jcsj_YuanQu>(db).Add(YuanQu);
                if (BolShiFouChengGong==false)
                {
                    nret = 1;
                    strFanHuiZhi = "添加院区出现错误，请检查！";
                    goto PROC_EXIT;
                }
                #endregion



            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 院区维护修改
        /// </summary>
        /// <param name="YuanQuUpdateClass"></param>
        /// <returns></returns>
        public string PutYuanQuUpdate(YuanQuUpdateClass YuanQuUpdateClass)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{YuanQuUpdateClass}】");

                BolShiFouChengGong = new SqlSugarRepository<jcsj_YuanQu>(db).Update(t => new jcsj_YuanQu
                {
                    ShiFouQiYong = YuanQuUpdateClass.strShiFouQiYong,
                    ShiFouZhuKu = YuanQuUpdateClass.strShiFouZhuKu
                },t => t.YuanQuMingCheng == YuanQuUpdateClass.strYuanQuMingCheng);

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改院区失败，请检查！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 院区维护查询
        /// </summary>
        /// <returns></returns>
        public string GetYuanQuSel()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                List<jcsj_YuanQu> ListYuanQu = new SqlSugarRepository<jcsj_YuanQu>(db).getAll().OrderBy(t => t.ShunXu).ToList();


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = new
                    {
                        ListXinXi= ListYuanQu
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
